package com.hup.minicontroller.client.view;

import android.bluetooth.BluetoothDevice;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hup.minicontroller.client.R;
import com.hup.minicontroller.client.model.LanBroadcastServer;
import com.hup.minicontroller.client.model.ScannedServerItem;
import com.hup.minicontroller.client.view.ScannedServerItemAdapter.MyViewHolder;
import com.hup.minicontroller.common.view.BaseAdapter;
import com.hup.minicontroller.common.view.BaseViewHolder;

import java.util.Collection;
import java.util.Iterator;

import butterknife.BindView;
import lombok.Setter;

public class ScannedServerItemAdapter extends BaseAdapter<ScannedServerItem, MyViewHolder> {

    @Setter
    private ConnectButtonListener connectButtonListener;

    @Override
    public int getLayoutResId() {
        return R.layout.item_scanned_server;
    }

    @Override
    public MyViewHolder createHolder() {
        return new MyViewHolder(connectButtonListener);
    }

    public void setLanBroadcastServer(Collection<LanBroadcastServer> broadcastServers) {
        //data.removeIf(),移除旧item
        Iterator<ScannedServerItem> it = data.iterator();
        while (it.hasNext()) if (it.next().isLan()) it.remove();
        //在列表前面
        addNewData(ScannedServerItem.cast(broadcastServers));
    }

    public void setBluetoothDevice(Collection<BluetoothDevice> bluetoothDevices) {
        Iterator<ScannedServerItem> it = data.iterator();
        while (it.hasNext()) if (!it.next().isLan()) it.remove();
        //在列表后面
        addMoreData(ScannedServerItem.cast2(bluetoothDevices));
    }

    public abstract static class ConnectButtonListener implements OnClickListener {
        @Override
        public final void onClick(View v) {
            ScannedServerItem item = (ScannedServerItem) v.getTag();
            onConnect(item);
        }

        public abstract void onConnect(ScannedServerItem item);
    }

    public static class MyViewHolder implements BaseViewHolder<ScannedServerItem> {

        @BindView(R.id.imgIcon)
        ImageView imgIcon;
        @BindView(R.id.txtServerName)
        TextView txtServerName;
        @BindView(R.id.txtAddress)
        TextView txtAddress;
        @BindView(R.id.btnConnect)
        Button btnConnect;

        private ConnectButtonListener connectButtonListener;

        MyViewHolder(ConnectButtonListener connectButtonListener) {
            this.connectButtonListener = connectButtonListener;
        }

        @Override
        public void onBindView(View view) {
            btnConnect.setOnClickListener(connectButtonListener);
        }

        @Override
        public void bindData(int position, ScannedServerItem item) {
            if (item.isLan()) {
                imgIcon.setImageResource(R.drawable.icon_lan);
                LanBroadcastServer lanBroadcastServer = item.getLanBroadcastServer();
                txtServerName.setText(lanBroadcastServer.getServerName());
                txtAddress.setText(lanBroadcastServer.getAddress());
            } else {
                imgIcon.setImageResource(R.drawable.icon_bluetooth);
                BluetoothDevice bluetoothDevice = item.getBluetoothDevice();
                txtServerName.setText(bluetoothDevice.getName());
                txtAddress.setText("");
            }
            btnConnect.setTag(item);
        }
    }

}
